home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group19]VCL Source Professional / IvMulti.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-09  |  71.9 KB  |  2,744 lines

  1. unit IvMulti;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8. {$IFDEF WIN32}
  9.   Windows,
  10. {$ELSE}
  11.   WinTypes, WinProcs,
  12. {$ENDIF}
  13. {$IFDEF _DEBUG}
  14.   DebugFil,
  15. {$ENDIF}
  16.   SysUtils, Classes, Dialogs, Forms, Controls, Graphics, TypInfo,
  17.   IvDictio, IvCommon;
  18.  
  19. const
  20.   TARGETS_C = 'Targets.txt';
  21.  
  22.   TARGETS_VERSION_C = 1;
  23.  
  24. type
  25.   TIvTargetType = (ivttInclude, ivttExclude);
  26.  
  27.   TIvTargetProperty = class(TObject)
  28.   protected
  29.     FTargetClassName: String;
  30.     FTargetPropertyName: String;
  31.     FTargetType: TIvTargetType;
  32.  
  33.   public
  34.     constructor Create(
  35.       const targetClassName, targetPropertyName: String;
  36.       targetType: TIvTargetType);
  37.     constructor CreateDefault;
  38.  
  39.     procedure Assign(target: TIvTargetProperty);
  40.     function IsEqual(target: TIvTargetProperty): Boolean;
  41.     function Copy: TIvTargetProperty;
  42.  
  43.     function IsDefault: Boolean;
  44.     procedure SetDefault;
  45.  
  46.     property TargetClassName: String read FTargetClassName write FTargetClassName;
  47.     property TargetPropertyName: String read FTargetPropertyName write FTargetPropertyName;
  48.     property TargetType: TIvTargetType read FTargetType write FTargetType;
  49.   end;
  50.  
  51.   TIvTargetProperties = class(TPersistent)
  52.   protected
  53.     FExcludeCount: Integer;
  54.     FItems: TList;
  55.  
  56.     function GetCount: Integer;
  57.     function GetItems(index: Integer): TIvTargetProperty;
  58.  
  59.     procedure ReadData(reader: TReader);
  60.  
  61.     procedure DefineProperties(Filer: TFiler); override;
  62.  
  63.     function DoesMatch(target: TIvTargetProperty; const obj, name: String): Boolean;
  64.  
  65.   public
  66.     constructor Create;
  67.     destructor Destroy; override;
  68.  
  69.     procedure Assign(source: TPersistent); override;
  70.     function IsEqual(targets: TIvTargetProperties): Boolean;
  71.     function Copy: TIvTargetProperties;
  72.  
  73.     function IsDefault: Boolean;
  74.     procedure SetDefault;
  75.  
  76.     function Find(item: TIvTargetProperty): Integer;
  77.  
  78.     function Add(item: TIvTargetProperty): Integer;
  79.     procedure Delete(index: integer);
  80.  
  81.     function IsObjectInTargets(const obj: String): Boolean;
  82.     function IsComponentInTargets(const component: TComponent): Boolean;
  83.     function IsPropertyInTargets(const obj, name: String): Boolean;
  84.     function IsPropertyExcluded(const obj, name: String): Boolean;
  85.  
  86.     property Count: Integer read GetCount;
  87.     property ExcludeCount: Integer read FExcludeCount write FExcludeCount;
  88.     property Items[index: Integer]: TIvTargetProperty read GetItems; default;
  89.   end;
  90.  
  91.   { TIvTranslator }
  92.  
  93.   TIvTranslator = class;
  94.  
  95.   TIvTranslateNotifyEvent = procedure(translator: TIvTranslator; component: TComponent) of object;
  96.  
  97.   TIvRestriction = (ivrBuildInTranslation, ivrCustomTranslation, ivrFlip,
  98.     ivrReadingOrder, ivrThisComponentOnly, ivrCharset);
  99.   TIvRestrictions = set of TIvRestriction;
  100.  
  101.   TIvRestrictComponentEvent = procedure(
  102.     translator: TIvTranslator;
  103.     component: TComponent;
  104.     var restrictions: TIvRestrictions) of object;
  105.   TIvRestrictObjectEvent = procedure(
  106.     translator: TIvTranslator;
  107.     obj: TObject;
  108.     var translate: Boolean) of object;
  109.   TIvRestrictPropertyEvent = procedure(
  110.     translator: TIvTranslator;
  111.     obj: TObject;
  112.     const name: String;
  113.     var translate: Boolean) of object;
  114.  
  115.   TIvTranslatorOption = (ivtoAutoTranslate, ivtoCheckFont, ivtoScaleMultiByte,
  116.     ivtoMirrorBiDirectional, ivtoChangeFontCharset, ivtoUpdateLocaleProperty,
  117.     ivtoAutoOpen, ivtoTranslateSystemMenu);
  118.   TIvTranslatorOptions = set of TIvTranslatorOption;
  119.  
  120.   TIvReadingOrder = (ivroLeftToRight, ivroRightToLeft);
  121.  
  122.   TIvChangeReadingOrderEvent = procedure(
  123.     translator: TIvTranslator;
  124.     component: TComponent;
  125.     readingOrder: TIvReadingOrder) of object;
  126.  
  127.   TIvBidirectionalState = class(TObject)
  128.   public
  129. {$IFDEF IVVB}
  130.     DefaultFontName: String;
  131.     DefaultFontSize: longint;
  132. {$ENDIF}
  133.     Control: TControl;
  134.     Flipped: Boolean;
  135.     OriginalLeft: Integer;
  136.     ReadingOrder: TIvReadingOrder;
  137.  
  138.     constructor Create(const control: TControl);
  139.   end;
  140.  
  141.   TIvTranslatorTableItem = class(TObject)
  142.   public
  143.     Obj: TObject;
  144.     Component: String;
  145.     PropertyName: String;
  146.     Native: String;
  147.     Current: String;
  148.  
  149.     constructor Create(
  150.       obj: TObject;
  151.       const component, propertyName, native, current: String);
  152.   end;
  153.  
  154.   TIvTranslatorTable = class(TObject)
  155.   protected
  156.     FRows: TList;
  157.  
  158.     function GetCount: Integer;
  159.     function GetItem(row: Integer): TIvTranslatorTableItem;
  160.  
  161.   public
  162.     constructor Create;
  163.     destructor Destroy; override;
  164.  
  165.     procedure Add(item: TIvTranslatorTableItem);
  166.     procedure Clear;
  167.  
  168.     property Count: Integer read GetCount;
  169.     property Items[row: Integer]: TIvTranslatorTableItem read GetItem;
  170.   end;
  171.  
  172.   TIvModule = class(TComponent)
  173.   public
  174.     function TranslateComponent(
  175.       translator: TIvTranslator;
  176.       component: TComponent): Boolean; virtual;
  177.  
  178.     function FlipControl(
  179.       translator: TIvTranslator;
  180.       control: TControl;
  181.       state: TIvBidirectionalState): Boolean; virtual;
  182.  
  183.     function UnFlipControl(
  184.       translator: TIvTranslator;
  185.       control: TControl;
  186.       state: TIvBidirectionalState): Boolean; virtual;
  187.  
  188.     function ChangeComponentReadingOrder(
  189.       translator: TIvTranslator;
  190.       component: TComponent): Boolean; virtual;
  191.   end;
  192.  
  193.   TIvModules = class(TObject)
  194.   private
  195.     FItems: TList;
  196.  
  197.     function GetCount: Integer;
  198.     function GetItems(index: Integer): TIvModule;
  199.  
  200.   public
  201.     constructor Create;
  202.     destructor Destroy; override;
  203.  
  204.     procedure Add(item: TIvModule);
  205.  
  206.     function TranslateComponent(
  207.       translator: TIvTranslator;
  208.       component: TComponent): Boolean;
  209.  
  210. {$IFDEF IVPRO32}
  211.     function FlipControl(
  212.       translator: TIvTranslator;
  213.       control: TControl;
  214.       state: TIvBidirectionalState): Boolean;
  215.  
  216.     function UnFlipControl(
  217.       translator: TIvTranslator;
  218.       control: TControl;
  219.       state: TIvBidirectionalState): Boolean;
  220.  
  221.     function ChangeComponentReadingOrder(
  222.       translator: TIvTranslator;
  223.       component: TComponent): Boolean;
  224. {$ENDIF}
  225.  
  226.     property Count: Integer read GetCount;
  227.     property Items[index: Integer]: TIvModule read GetItems; default;
  228.   end;
  229.  
  230.   TIvTranslator = class(TIvCustomTranslator)
  231.   protected
  232.     FNativeLayout: TIvReadingOrder;
  233.     FReadingOrder: TIvReadingOrder;
  234.     FHost: TComponent;
  235.     FTranslatorTable: TIvTranslatorTable;
  236.     FTargets: TIvTargetProperties;
  237.     FOptions: TIvTranslatorOptions;
  238.     FRestrictions: TIvRestrictions;
  239.     FDefaultFontName: String;
  240.     FDefaultFontSize: Integer;
  241.     FMultiByteScale: Integer;
  242.     FBidirectionalStates: TList;
  243.     FOnTranslate: TIvTranslateNotifyEvent;
  244.     FOnScale: TIvTranslateNotifyEvent;
  245.     FOnRestrictComponent: TIvRestrictComponentEvent;
  246.     FOnRestrictObject: TIvRestrictObjectEvent;
  247.     FOnRestrictProperty: TIvRestrictPropertyEvent;
  248.  
  249.     function GetAutoOpen: Boolean;
  250.     procedure SetAutoOpen(value: Boolean);
  251.  
  252.     function GetHost: TComponent; virtual;
  253.  
  254.     procedure SetTargets(value: TIvTargetProperties);
  255.  
  256.     procedure Notification(component: TComponent; operation: TOperation); override;
  257.  
  258.     procedure TranslateHost; override;
  259.     procedure TranslateObject(obj: TObject; restrictions: TIvRestrictions); virtual;
  260.     procedure TranslateSubComponent(
  261.       component: TComponent;
  262.       parentRestrictions: TIvRestrictions); virtual;
  263.  
  264.     procedure CheckHostFont; virtual;
  265.  
  266.     function GetBidirectionalState(control: TControl; allocNew: Boolean): TIvBidirectionalState;
  267.  
  268.     procedure FlipControl(control: TControl; state: TIvBidirectionalState); virtual;
  269.     procedure UnFlipControl(control: TControl; state: TIvBidirectionalState); virtual;
  270.  
  271.     procedure LanguageChanged(languageChanged, localeChanged: Boolean); override;
  272.  
  273.     procedure ReadTargets(reader: TReader);
  274.     procedure WriteTargets(writer: TWriter);
  275. {
  276.     procedure AddObjectTargets(
  277.       obj: TObject;
  278.       targets: TIvTargetProperties;
  279.       prompt: Boolean);
  280. }
  281.     procedure DefineProperties(Filer: TFiler); override;
  282.  
  283.     procedure Loaded; override;
  284.  
  285. {$IFDEF WIN32}
  286.     function GetSystemMenuWinHandle: THandle; override;
  287. {$ENDIF}
  288.  
  289.     { Override these in your derived translators }
  290.  
  291.     procedure TranslateComponent(component: TComponent); virtual;
  292.     procedure ChangeComponentReadingOrder(component: TComponent); virtual;
  293.  
  294.   public
  295.     constructor Create(owner: TComponent); override;
  296.     destructor Destroy; override;
  297.  
  298.     procedure Translate; override;
  299.     procedure Unbind; override;
  300.     procedure UnbindAndSetNative; override;
  301.  
  302.     procedure AddObjectTargets(
  303.       obj: TObject;
  304.       targets: TIvTargetProperties;
  305.       prompt: Boolean);
  306.  
  307.     function DoTranslateString(
  308.       obj: TObject;
  309.       const propertyName, value: String): String;
  310.     function DoTranslateContextString(
  311.       obj: TObject;
  312.       const component, propertyName, value: String): String;
  313.  
  314.     procedure DoTranslateStrings(
  315.       obj: TObject;
  316.       const component, propertyName: String; value: TStrings);
  317.  
  318.     procedure AddTranslation(
  319.       obj: TObject;
  320.       const component, propertyName, value: String);
  321.  
  322.     procedure Open(host: TComponent); virtual;
  323.     procedure Close; virtual;
  324.  
  325.     procedure DetectTargets(
  326.       targets: TIvTargetProperties;
  327.       prompt: Boolean);
  328.  
  329.     function GetNativePropertyValue(
  330.       component: TObject;
  331.       const propertyName: String): String;
  332.     function GetNativePropertyValueEx(
  333.       component: TObject;
  334.       const propertyName, currentValue: String): String;
  335.  
  336.     procedure UpdateControl(control: TControl); virtual;
  337.     procedure UpdateControls; virtual;
  338.  
  339.     property Host: TComponent read GetHost;
  340.     property ReadingOrder: TIvReadingOrder read FReadingOrder;
  341.     property TranslatorTable: TIvTranslatorTable read FTranslatorTable;
  342.  
  343.   published
  344.     property NativeLayout: TIvReadingOrder read FNativeLayout write FNativeLayout default ivroLeftToRight;
  345.     property Targets: TIvTargetProperties read FTargets write SetTargets;
  346.     property Options: TIvTranslatorOptions read FOptions write FOptions default
  347.       [ivtoAutoTranslate, ivtoCheckFont, ivtoScaleMultiByte, ivtoMirrorBiDirectional,
  348.        ivtoChangeFontCharset, ivtoAutoOpen, ivtoTranslateSystemMenu];
  349.     property MultiByteScale: Integer read FMultiByteScale write FMultiByteScale default 100;
  350.     property OnTranslate: TIvTranslateNotifyEvent read FOnTranslate write FOnTranslate;
  351.     property OnScale: TIvTranslateNotifyEvent read FOnScale write FOnScale;
  352.     property OnRestrictComponent: TIvRestrictComponentEvent read FOnRestrictComponent write FOnRestrictComponent;
  353.     property OnRestrictObject: TIvRestrictObjectEvent read FOnRestrictObject write FOnRestrictObject;
  354.     property OnRestrictProperty: TIvRestrictPropertyEvent read FOnRestrictProperty write FOnRestrictProperty;
  355.   end;
  356.  
  357. procedure IvCenterControl(parent, control: TControl);
  358.  
  359. var
  360.   Modules: TIvModules;
  361.  
  362. implementation
  363.  
  364. uses
  365. {$IFDEF IVVB}
  366.   CompOCX,
  367. {$ENDIF}
  368. {$IFDEF IVWIDE}
  369.   Checklst,
  370. {$ENDIF}
  371.   Messages, StdCtrls, Menus,
  372.   IvParser;
  373.  
  374. const
  375.   NO_DICTIONARY_C = 'No dictionary has been assigned to the translator component (%s on "%s" form)';
  376.   NO_TARGETS_C = 'No target properties has been assigned to the translator component (%s on "%s" form)';
  377.   INVALID_HOST_C = 'The host component (owner) of the translator must be a TControl (%s on "%s" form)';
  378.  
  379. { TIvTargetProperty }
  380.  
  381. constructor TIvTargetProperty.Create(
  382.   const targetClassName, targetPropertyName: String;
  383.   targetType: TIvTargetType);
  384. begin
  385.   inherited Create;
  386.   FTargetClassName := targetClassName;
  387.   FTargetPropertyName := targetPropertyName;
  388.   FTargetType := targetType;
  389. end;
  390.  
  391. constructor TIvTargetProperty.CreateDefault;
  392. begin
  393.   Create('', '', ivttInclude);
  394. end;
  395.  
  396. function TIvTargetProperty.Copy: TIvTargetProperty;
  397. begin
  398.   Result := TIvTargetProperty.Create(FTargetClassName, FTargetPropertyName, FTargetType);
  399. end;
  400.  
  401. procedure TIvTargetProperty.Assign(target: TIvTargetProperty);
  402. begin
  403.   FTargetClassName := target.FTargetClassName;
  404.   FTargetPropertyName := target.FTargetPropertyName;
  405.   FTargetType := target.FTargetType;
  406. end;
  407.  
  408. function TIvTargetProperty.IsEqual(target: TIvTargetProperty): Boolean;
  409. begin
  410.   Result :=
  411.     (FTargetClassName = target.FTargetClassName) and
  412.     (FTargetPropertyName = target.FTargetPropertyName) and
  413.     (FTargetType = target.FTargetType);
  414. end;
  415.  
  416. function TIvTargetProperty.IsDefault: Boolean;
  417. begin
  418.   Result := (FTargetPropertyName = '') and
  419.     (FTargetClassName = '') and
  420.     (FTargetType = ivttInclude);
  421. end;
  422.  
  423. procedure TIvTargetProperty.SetDefault;
  424. begin
  425.   FTargetClassName := '';
  426.   FTargetPropertyName := '';
  427.   FTargetType := ivttInclude;
  428. end;
  429.  
  430.  
  431. { TIvTargetProperties }
  432.  
  433. constructor TIvTargetProperties.Create;
  434. begin
  435.   inherited Create;
  436.   FExcludeCount := 0;
  437.   FItems := TList.Create;
  438. end;
  439.  
  440. destructor TIvTargetProperties.Destroy;
  441. begin
  442.   SetDefault;
  443.   FItems.Free;
  444.   inherited Destroy;
  445. end;
  446.  
  447. procedure TIvTargetProperties.Assign(source: TPersistent);
  448. var
  449.   i: Integer;
  450. begin
  451.   if source is TIvTargetProperties then
  452.   begin
  453.     SetDefault;
  454.     FExcludeCount := TIvTargetProperties(source).FExcludeCount;
  455.     for i := 0 to TIvTargetProperties(source).Count - 1 do
  456.       Add(TIvTargetProperty(TIvTargetProperties(source).Items[i].Copy));
  457.     Exit;
  458.   end;
  459.   inherited Assign(source);
  460. end;
  461.  
  462. function TIvTargetProperties.IsEqual(targets: TIvTargetProperties): Boolean;
  463. var
  464.   i, j: Integer;
  465.   found: Boolean;
  466. begin
  467.   if Count = targets.Count then
  468.   begin
  469.     for i := 0 to Count - 1 do
  470.     begin
  471.       found := False;
  472.  
  473.       for j := 0 to targets.Count - 1 do
  474.       begin
  475.         if Items[i].IsEqual(targets.Items[j]) then
  476.         begin
  477.           found := True;
  478.           Break;
  479.         end;
  480.       end;
  481.  
  482.       if not found then
  483.       begin
  484.         Result := False;
  485.         Exit;
  486.       end;
  487.     end;
  488.     
  489.     Result := True;
  490.   end
  491.   else
  492.     Result := False;
  493. end;
  494.  
  495. procedure TIvTargetProperties.ReadData(reader: TReader);
  496. var
  497.   i, count: Integer;
  498.   target: TIvTargetProperty;
  499. begin
  500.   SetDefault;
  501.   reader.ReadListBegin;
  502.   count := reader.ReadInteger;
  503.   for i := 0 to count - 1 do
  504.   begin
  505.     target := TIvTargetProperty.CreateDefault;
  506.     target.TargetClassName := reader.ReadString;
  507.     target.TargetPropertyName := reader.ReadString;
  508.     FItems.Add(target);
  509.   end;
  510.   reader.ReadListEnd;
  511. end;
  512.  
  513. procedure TIvTargetProperties.DefineProperties(filer: TFiler);
  514. begin
  515.   inherited DefineProperties(Filer);
  516.   Filer.DefineProperty('Data', ReadData, nil, filer is TReader);
  517. end;
  518.  
  519. function TIvTargetProperties.GetCount: Integer;
  520. begin
  521.   Result := FItems.Count;
  522. end;
  523.  
  524. function TIvTargetProperties.GetItems(index: Integer): TIvTargetProperty;
  525. begin
  526.   Result := FItems[index];
  527. end;
  528.  
  529. function TIvTargetProperties.Find(item: TIvTargetProperty): Integer;
  530. var
  531.   i: Integer;
  532. begin
  533.   for i := 0 to Count - 1 do
  534.     if Items[i].IsEqual(item) then
  535.     begin
  536.       Result := i;
  537.       Exit;
  538.     end;
  539.     
  540.   Result := -1;
  541. end;
  542.  
  543. function TIvTargetProperties.Add(item: TIvTargetProperty): Integer;
  544. begin
  545.   Result := FItems.Add(item);
  546.   if item.TargetType = ivttExclude then
  547.     Inc(FExcludeCount);
  548. end;
  549.  
  550. procedure TIvTargetProperties.Delete(index: integer);
  551. begin
  552.   if Items[index].TargetType = ivttExclude then
  553.     Dec(FExcludeCount);
  554.   FItems.Delete(index);
  555. end;
  556.  
  557. function TIvTargetProperties.Copy: TIvTargetProperties;
  558. begin
  559.   Result := TIvTargetProperties.Create;
  560.   Result.Assign(Self);
  561. end;
  562.  
  563. function TIvTargetProperties.IsDefault: Boolean;
  564. begin
  565.   Result := Count = 0;
  566. end;
  567.  
  568. procedure TIvTargetProperties.SetDefault;
  569. var
  570.   I: Integer;
  571. begin
  572.   FExcludeCount := 0;
  573.   for i := 0 to Count - 1 do
  574.     Items[i].Free;
  575.   FItems.Clear;
  576. end;
  577.  
  578. function TIvTargetProperties.DoesMatch(
  579.   target: TIvTargetProperty;
  580.   const obj, name: String): Boolean;
  581. begin
  582.   { If the target class name is null or matches to the object and the target
  583.     propety name matches to the name the property should be translated.
  584.  
  585.     Similary the property should be translated if a target is a class target.
  586.     This means a target with no property name. In such a case all string
  587.     properties of the class should be translated. }
  588.  
  589.   Result :=
  590.     (((target.TargetClassName = '') or
  591.       (CompareText(target.TargetClassName, obj) = 0)) and
  592.     (CompareText(name, target.TargetPropertyName) = 0)) or
  593.     ((CompareText(target.TargetClassName, obj) = 0) and
  594.      (target.TargetPropertyName = ''));
  595. end;
  596.  
  597. function TIvTargetProperties.IsPropertyExcluded(const obj, name: String): Boolean;
  598. var
  599.   i: Integer;
  600.   target: TIvTargetProperty;
  601. begin
  602.   Result := False;
  603.  
  604.   if FExcludeCount > 0 then
  605.   begin
  606.     for i := 0 to Count - 1 do
  607.     begin
  608.       target := Items[i];
  609.       if (target.TargetType = ivttExclude) and DoesMatch(target, obj, name) then
  610.       begin
  611.         Result := True;
  612.         Break;
  613.       end;
  614.     end;
  615.   end;
  616. end;
  617.  
  618. function TIvTargetProperties.IsPropertyInTargets(const obj, name: String): Boolean;
  619. var
  620.   i: Integer;
  621.   target: TIvTargetProperty;
  622. begin
  623.   Result := False;
  624.  
  625.   { If the targets properties contains exclude targets checks them first }
  626.  
  627.   if IsPropertyExcluded(obj, name) then
  628.     Exit;
  629.  
  630.   { Checks if any include target matches the give property }
  631.  
  632.   for i := 0 to Count - 1 do
  633.   begin
  634.     target := Items[i];
  635.     if (target.TargetType = ivttInclude) and DoesMatch(target, obj, name) then
  636.     begin
  637.       Result := True;
  638.       Exit;
  639.     end;
  640.   end;
  641. end;
  642.  
  643. function TIvTargetProperties.IsObjectInTargets(const obj: String): Boolean;
  644. var
  645.   i: Integer;
  646.   target: TIvTargetProperty;
  647.  
  648.   function DoesMatch: Boolean;
  649.   begin
  650.     Result :=
  651.       (CompareText(target.TargetClassName, obj) = 0) and
  652.       (target.TargetPropertyName = '');
  653.   end;
  654.  
  655. begin
  656.   Result := False;
  657.  
  658.   { If the targets properties contains exclude targets checks them first }
  659.  
  660.   if FExcludeCount > 0 then
  661.   begin
  662.     for i := 0 to Count - 1 do
  663.     begin
  664.       target := Items[i];
  665.       if (target.TargetType = ivttExclude) and DoesMatch then
  666.         Exit;
  667.     end;
  668.   end;
  669.  
  670.   { Checks if any include target matches the give property }
  671.  
  672.   for i := 0 to Count - 1 do
  673.   begin
  674.     target := Items[i];
  675.     if (target.TargetType = ivttInclude) and DoesMatch then
  676.     begin
  677.       Result := True;
  678.       Exit;
  679.     end;
  680.   end;
  681. end;
  682.  
  683. function TIvTargetProperties.IsComponentInTargets(const component: TComponent): Boolean;
  684. var
  685.   i: Integer;
  686.   target: TIvTargetProperty;
  687.  
  688.   function DoesMatch: Boolean;
  689.   begin
  690.     Result :=
  691.       (CompareText(target.TargetClassName, component.ClassName) = 0) and
  692.       (CompareText(target.TargetPropertyName, '') = 0);
  693.   end;
  694.  
  695. begin
  696.   Result := False;
  697.  
  698.   { If the targets properties contains exclude targets checks them first }
  699.  
  700.   if FExcludeCount > 0 then
  701.   begin
  702.     for i := 0 to Count - 1 do
  703.     begin
  704.       target := Items[i];
  705.       if (target.TargetType = ivttExclude) and DoesMatch then
  706.         Exit;
  707.     end;
  708.   end;
  709.  
  710.   { Checks if any include target matches the give property }
  711.  
  712.   for i := 0 to Count - 1 do
  713.   begin
  714.     target := Items[i];
  715.     if (target.TargetType = ivttInclude) and DoesMatch then
  716.     begin
  717.       Result := True;
  718.       Exit;
  719.     end;
  720.   end;
  721. end;
  722.  
  723.  
  724. { TIvBidirectionalState }
  725.  
  726. constructor TIvBidirectionalState.Create(const control: TControl);
  727. begin
  728.   inherited Create;
  729. {$IFDEF IVPRO}
  730.  {$IFDEF IVVB}
  731.   if TObject(control) is TComponentOCX then
  732.   begin
  733.     Self.Control := control;
  734.     OriginalLeft := TComponentOCX(control).Left;
  735.     DefaultFontName := TComponentOCX(control).FontName;
  736.     DefaultFontSize := TComponentOCX(control).FontSize;
  737.     Exit;
  738.   end;
  739.  {$ENDIF}
  740.  
  741.   Self.Control := control;
  742.   OriginalLeft := control.Left;
  743. {$ENDIF}
  744. end;
  745.  
  746.  
  747. { TIvTranslatorTable }
  748.  
  749. constructor TIvTranslatorTableItem.Create(obj: TObject; const component, propertyName, native, current: String);
  750. begin
  751.   inherited Create;
  752.   Self.obj := obj;
  753.   Self.component := component;
  754.   Self.propertyName := propertyName;
  755.   Self.native := native;
  756.   Self.current := current;
  757. end;
  758.  
  759. constructor TIvTranslatorTable.Create;
  760. begin
  761.   inherited Create;
  762.   FRows := TList.Create;
  763. end;
  764.  
  765. destructor TIvTranslatorTable.Destroy;
  766. begin
  767.   Clear;
  768.   FRows.Free;
  769.   inherited Destroy;
  770. end;
  771.  
  772. procedure TIvTranslatorTable.Clear;
  773. var
  774.   i: Integer;
  775. begin
  776.   for i := 0 to FRows.Count - 1 do
  777.     TIvTranslatorTableItem(FRows[i]).Free;
  778.   FRows.Clear;
  779. end;
  780.  
  781. function TIvTranslatorTable.GetCount: Integer;
  782. begin
  783.   Result := FRows.Count;
  784. end;
  785.  
  786. function TIvTranslatorTable.GetItem(row: Integer): TIvTranslatorTableItem;
  787. begin
  788.   Result := FRows[row];
  789. end;
  790.  
  791. procedure TIvTranslatorTable.Add(item: TIvTranslatorTableItem);
  792. begin
  793.   FRows.Add(item);
  794. end;
  795.  
  796.  
  797. { TIvModule }
  798.  
  799. function TIvModule.TranslateComponent(
  800.   translator: TIvTranslator;
  801.   component: TComponent): Boolean;
  802. begin
  803.   Result := False;
  804. end;
  805.  
  806. function TIvModule.FlipControl(
  807.   translator: TIvTranslator;
  808.   control: TControl;
  809.   state: TIvBidirectionalState): Boolean;
  810. begin
  811.   Result := False;
  812. end;
  813.  
  814. function TIvModule.UnFlipControl(
  815.   translator: TIvTranslator;
  816.   control: TControl;
  817.   state: TIvBidirectionalState): Boolean;
  818. begin
  819.   Result := False;
  820. end;
  821.  
  822. function TIvModule.ChangeComponentReadingOrder(
  823.   translator: TIvTranslator;
  824.   component: TComponent): Boolean;
  825. begin
  826.   Result := False;
  827. end;
  828.  
  829.  
  830. { TIvModules }
  831.  
  832. constructor TIvModules.Create;
  833. begin
  834.   inherited Create;
  835.   FItems := TList.Create;
  836. end;
  837.  
  838. destructor TIvModules.Destroy;
  839. begin
  840.   FItems.Free;
  841.   inherited Destroy;
  842. end;
  843.  
  844. function TIvModules.GetCount: Integer;
  845. begin
  846.   Result := FItems.Count;
  847. end;
  848.  
  849. function TIvModules.GetItems(index: Integer): TIvModule;
  850. begin
  851.   Result := FItems[index];
  852. end;
  853.  
  854. procedure TIvModules.Add(item: TIvModule);
  855. var
  856.   i: Integer;
  857. begin
  858.   { Adds the module the the module list if it is not already there. }
  859.  
  860.   for i := 0 to Count - 1 do
  861.     if Items[i].ClassType = item.ClassType then
  862.       Exit;
  863.  
  864.   FItems.Add(item);
  865. end;
  866.  
  867. function TIvModules.TranslateComponent(
  868.   translator: TIvTranslator;
  869.   component: TComponent): Boolean;
  870. var
  871.   i: Integer;
  872. begin
  873.   Result := False;
  874.   for i := 0 to Count - 1 do
  875.     if Items[i].TranslateComponent(translator, component) then
  876.     begin
  877.       Result := True;
  878.       Break;
  879.     end;
  880. end;
  881.  
  882. {$IFDEF IVPRO32}
  883. function TIvModules.FlipControl(
  884.   translator: TIvTranslator;
  885.   control: TControl;
  886.   state: TIvBidirectionalState): Boolean;
  887. var
  888.   i: Integer;
  889. begin
  890.   Result := False;
  891.   for i := 0 to Count - 1 do
  892.     if Items[i].FlipControl(translator, control, state) then
  893.     begin
  894.       Result := True;
  895.       Break;
  896.     end;
  897. end;
  898.  
  899. function TIvModules.UnFlipControl(
  900.   translator: TIvTranslator;
  901.   control: TControl;
  902.   state: TIvBidirectionalState): Boolean;
  903. var
  904.   i: Integer;
  905. begin
  906.   Result := False;
  907.   for i := 0 to Count - 1 do
  908.     if Items[i].UnFlipControl(translator, control, state) then
  909.     begin
  910.       Result := True;
  911.       Break;
  912.     end;
  913. end;
  914.  
  915. function TIvModules.ChangeComponentReadingOrder(
  916.   translator: TIvTranslator;
  917.   component: TComponent): Boolean;
  918. var
  919.   i: Integer;
  920. begin
  921.   Result := False;
  922.   for i := 0 to Count - 1 do
  923.     if Items[i].ChangeComponentReadingOrder(translator, component) then
  924.     begin
  925.       Result := True;
  926.       Exit;
  927.     end;
  928. end;
  929. {$ENDIF}
  930.  
  931.  
  932. { TIvTranslator }
  933.  
  934. constructor TIvTranslator.Create(owner: TComponent);
  935. var
  936.   detectType: TIvDetectType;
  937. begin
  938.   inherited Create(owner);
  939.  
  940.   FNativeLayout := ivroLeftToRight;
  941.   FHost := nil;
  942.   FTranslatorTable := TIvTranslatorTable.Create;
  943.   FTargets := TIvTargetProperties.Create;
  944.  
  945.   FOptions := [ivtoAutoTranslate, ivtoCheckFont, ivtoScaleMultiByte,
  946.     ivtoMirrorBiDirectional, ivtoChangeFontCharset, ivtoAutoOpen, ivtoTranslateSystemMenu];
  947.  
  948.   FMultiByteScale := 100;
  949.  
  950. {$IFDEF IVPRO32}
  951.   FBidirectionalStates := TList.Create;
  952. {$ENDIF}
  953.  
  954.   if IsDesignTime and not (csReading in owner.ComponentState) and not (csLoading in owner.ComponentState) then
  955.   begin
  956.     detectType :=
  957.       TIvDetectType(StrToIntDef(GetMLRegistryValue(DETECT_C, IntToStr(Integer(ivdtEnabled))), Integer(ivdtEnabled)));
  958.     if (detectType = ivdtEnabled) or ((detectType = ivdtPrompt) and
  959.       (MessageDlg(
  960.         'Do you want the translator component to detect the default targets?',
  961.         mtInformation,
  962.         [mbYes, mbNo],
  963.         0) = mrYes)) then
  964.     begin
  965.       DetectTargets(FTargets, False);
  966.     end;
  967.   end;
  968. end;
  969.  
  970. destructor TIvTranslator.Destroy;
  971. begin
  972.   Unbind;
  973.   FTranslatorTable.Free;
  974.   FTargets.Free;
  975. {$IFDEF IVPRO32}
  976.   while FBidirectionalStates.Count > 0 do
  977.   begin
  978.     TObject(FBidirectionalStates[0]).Free;
  979.     FBidirectionalStates.Delete(0);
  980.   end;
  981.   FBidirectionalStates.Free;
  982. {$ENDIF}
  983.   inherited Destroy;
  984. end;
  985.  
  986. function TIvTranslator.GetAutoOpen: Boolean;
  987. begin
  988.   Result := ivtoAutoOpen in FOptions;
  989. end;
  990.  
  991. procedure TIvTranslator.SetAutoOpen(value: Boolean);
  992. begin
  993.   if value then
  994.     FOptions := FOptions + [ivtoAutoOpen]
  995.   else
  996.     FOptions := FOptions - [ivtoAutoOpen];
  997. end;
  998.  
  999. function TIvTranslator.GetHost: TComponent;
  1000. begin
  1001.   if FHost <> nil then
  1002.     Result := FHost
  1003.   else
  1004.     Result := Owner;
  1005. end;
  1006.  
  1007. procedure TIvTranslator.SetTargets(value: TIvTargetProperties);
  1008. begin
  1009.   FTargets.Assign(value);
  1010. end;
  1011.  
  1012. function TIvTranslator.DoTranslateString(
  1013.   obj: TObject;
  1014.   const propertyName, value: String): String;
  1015. begin
  1016.   Result := DoTranslateContextString(obj, '', propertyName, value);
  1017. end;
  1018.  
  1019. function TIvTranslator.DoTranslateContextString(
  1020.   obj: TObject;
  1021.   const component, propertyName, value: String): String;
  1022. var
  1023.   i: Integer;
  1024.   item: TIvTranslatorTableItem;
  1025.  
  1026.   function Translate(const str, component: String): String;
  1027.   var
  1028.     l, h, i, c: Integer;
  1029.     ok: Boolean;
  1030.     thisStr: String;
  1031.     translation: TIvTranslation;
  1032. {$IFNDEF IVVB}
  1033.     thisHost: TClass;
  1034. {$ENDIF}
  1035.   begin
  1036.     if FDictionary = nil then
  1037.     begin
  1038.       Result := str;
  1039.       Exit;
  1040.     end;
  1041.  
  1042.     { Tries to first find from the translation list }
  1043.  
  1044.     l := 0;
  1045.     h := FTranslations.Count - 1;
  1046.     thisStr := str + Host.Name + component;
  1047.     while l <= h do
  1048.     begin
  1049.       i := (l + h) div 2;
  1050.       translation := TIvTranslation(FTranslations[i]);
  1051.       c := SysUtils.CompareStr(translation.Key, thisStr);
  1052.       if c = 0 then
  1053.       begin
  1054.         if translation.Exists then
  1055.         begin
  1056.           Result := translation.Current;
  1057.           if Result = '' then
  1058.             Result := str;
  1059.         end
  1060.         else
  1061.           Result := str;
  1062.         Exit;
  1063.       end
  1064.       else if c < 0 then
  1065.         l := i + 1
  1066.       else
  1067.         h := i - 1;
  1068.     end;
  1069.  
  1070.     { Not found. Gets the translation from the dictionary. }
  1071.  
  1072.     if FDictionary.ContextType = [] then
  1073.     begin
  1074.       { Flat dictionary }
  1075.  
  1076.       Result := FDictionary.Translate(str);
  1077.     end
  1078.     else
  1079.     begin
  1080.       { Context sensitive dictionary }
  1081.  
  1082. {$IFDEF IVVB}
  1083.       ok := FDictionary.TranslateContextString(str, Host.Name, component, Result);
  1084. {$ELSE}
  1085.       thisHost := Host.ClassType;
  1086.       repeat
  1087.         ok := FDictionary.TranslateContextString(str, thisHost.ClassName, component, Result);
  1088.         thisHost := thisHost.ClassParent;
  1089.       until ok or (thisHost = TForm) or
  1090.   {$IFDEF IVWIDE}
  1091.         (thisHost = TCustomForm) or
  1092.   {$ENDIF}
  1093.         (thisHost = TComponent);
  1094. {$ENDIF}
  1095.  
  1096.       if ok then
  1097.         Result := FDictionary.CheckTranslation(str, Result, ok)
  1098.       else
  1099.       begin
  1100.         { Could not tranlsate ther string in a context sensitive way.
  1101.           Translates in a flat way. }
  1102.  
  1103.         Result := FDictionary.Translate(str);
  1104.       end;  
  1105.     end;
  1106.   end;
  1107.  
  1108. begin
  1109.   Result := value;
  1110.   if value = '' then
  1111.     Exit;
  1112.  
  1113.   { Scans the translator table to find the string.
  1114.     If finds the string, gets the native string and translates it. }
  1115.  
  1116.   for i := 0 to FTranslatorTable.Count - 1 do
  1117.   begin
  1118.     item := FTranslatorTable.Items[i];
  1119.     if (item.Obj = obj) and
  1120.       (item.PropertyName = propertyName) and
  1121.       (item.Current = value) then
  1122.     begin
  1123.       Result := Translate(item.Native, item.Component);
  1124.       item.Current := Result;
  1125.       Exit;
  1126.     end;
  1127.   end;
  1128.  
  1129.   { Not found. Translates the string and adds it to the table }
  1130.  
  1131.   Result := Translate(value, component);
  1132.   FTranslatorTable.Add(TIvTranslatorTableItem.Create(
  1133.     obj,
  1134.     component,
  1135.     propertyName,
  1136.     value,
  1137.     Result));
  1138. end;
  1139.  
  1140. procedure TIvTranslator.DoTranslateStrings(
  1141.   obj: TObject;
  1142.   const component, propertyName: String; value: TStrings);
  1143. var
  1144.   i: Integer;
  1145. begin
  1146.   if value.Count = 0 then
  1147.     Exit;
  1148.  
  1149.   value.BeginUpdate;
  1150.   try
  1151.     for i := 0 to value.Count - 1 do
  1152.       value[i] := DoTranslateContextString(obj, component, propertyName, value[i]);
  1153.   finally
  1154.     value.EndUpdate;
  1155.   end;  
  1156. end;
  1157.  
  1158. procedure TIvTranslator.AddTranslation(
  1159.   obj: TObject;
  1160.   const component, propertyName, value: String);
  1161. var
  1162.   i: Integer;
  1163.   item: TIvTranslatorTableItem;
  1164. begin
  1165.   for i := 0 to FTranslatorTable.Count - 1 do
  1166.   begin
  1167.     item := FTranslatorTable.Items[i];
  1168.     if (item.Obj = obj) and
  1169.       (item.PropertyName = propertyName) and
  1170.       (item.Current = value) then
  1171.     begin
  1172.       FTranslations.Add(TIvTranslation.CreateValue(item.Native, Host.Name, component));
  1173.       Exit;
  1174.     end;
  1175.   end;
  1176.  
  1177.   FTranslations.Add(TIvTranslation.CreateValue(value, Host.Name, component));
  1178. end;
  1179.  
  1180. procedure TIvTranslator.TranslateObject(obj: TObject; restrictions: TIvRestrictions);
  1181. var
  1182.   found, canTranslate, doTranslate: Boolean;
  1183.   i, j, k, oldIndex, locale: Integer;
  1184.   componentName, str: String;
  1185.   item: PPropInfo;
  1186.   list: TIvPropInfoList;
  1187.   strings: TStrings;
  1188.   objectProperty: TObject;
  1189.   component, ownerComponent: TComponent;
  1190.   filter: TTypeKinds;
  1191.   oldSelected: TList;
  1192. {$IFDEF WIN32}
  1193.   collection: TCollection;
  1194. {$ENDIF}
  1195. begin
  1196.   if (obj is TIvDictionary) or (obj is TIvTranslator) or (obj is TFont) then
  1197.     Exit;
  1198.  
  1199.   { Checks the restriction }
  1200.  
  1201.   canTranslate := True;
  1202.   if Assigned(FOnRestrictObject) then
  1203.     FOnRestrictObject(self, obj, canTranslate);
  1204.   if not canTranslate then
  1205.     Exit;
  1206.  
  1207.   { Gets the properties of the object }
  1208.  
  1209.   filter := [tkClass, tkString
  1210. {$IFDEF WIN32}
  1211.   , tkLString
  1212. {$ENDIF}
  1213.   ];
  1214.  
  1215.   if not (ivtsPreScanning in FState) and (ivtoUpdateLocaleProperty in Options)
  1216. {$IFDEF IVPRO32}
  1217.     or ((ivtsMirrored in State) and ((FDictionary = nil) or not IvIsLocaleBidirectional(Dictionary.LanguageLocale))) or
  1218.     (not (ivtsMirrored in State) and (FDictionary <> nil) and IvIsLocaleBidirectional(Dictionary.LanguageLocale))
  1219. {$ENDIF}
  1220.   then
  1221.   begin
  1222.     filter := filter + [tkInteger];
  1223.   end;
  1224.  
  1225.   list := TIvPropInfoList.Create(obj, filter);
  1226.   try
  1227.     componentName := '';
  1228.     if obj is TComponent then
  1229.     begin
  1230.       component := TComponent(obj);
  1231.       if component <> FHost then
  1232.         componentName := component.name;
  1233.     end
  1234.     else
  1235.       component := nil;
  1236.  
  1237.     for i := 0 to list.Count - 1 do
  1238.     begin
  1239.       { Checks if the property is read only.
  1240.         In that case it is not translated }
  1241.  
  1242.       item := list.Items[i];
  1243.       if (item^.SetProc = nil) or (item^.GetProc = nil) then
  1244.         Continue;
  1245.  
  1246.       { Checks if the translation of the property is restricted.
  1247.         In that case it is not translated }
  1248.  
  1249.       canTranslate := True;
  1250.       if Assigned(FOnRestrictProperty) then
  1251.         FOnRestrictProperty(self, obj, item^.Name, canTranslate);
  1252.       if not canTranslate then
  1253.         Continue;
  1254.  
  1255.       case item^.PropType^.Kind of
  1256. {$IFDEF WIN32}
  1257.         tkLString,
  1258. {$ENDIF}
  1259.         tkString:
  1260.         begin
  1261.           { A string property.
  1262.             The Name property of a component is not translated. }
  1263.  
  1264.           if ((component = nil) or (CompareText(item^.Name, 'name') <> 0)) and
  1265.             FTargets.IsPropertyInTargets(obj.ClassName, item^.Name) then
  1266.           begin
  1267.             str := GetStrProp(obj, item);
  1268.             if str <> '' then
  1269.               if ivtsPreScanning in FState then
  1270.                 AddTranslation(obj, componentName, item^.Name, str)
  1271.               else
  1272.                 SetStrProp(
  1273.                   obj,
  1274.                   item,
  1275.                   DoTranslateContextString(obj, componentName, item^.Name, str));
  1276.           end;
  1277.         end;
  1278.  
  1279.         tkInteger:
  1280.         begin
  1281.           { If the object is a locale aware object, updates the Locale property }
  1282.  
  1283.           if (component <> nil) and
  1284.             {(ivtoUpdateLocaleProperty in Options) and}
  1285.             (CompareText(item^.Name, 'locale') = 0) and
  1286.             not (ivrFlip in restrictions) then
  1287.           begin
  1288.             if FDictionary = nil then
  1289.               locale := 0
  1290.             else
  1291.               locale := FDictionary.LanguageLocale;
  1292.             SetOrdProp(obj, item, locale);
  1293.           end;
  1294.         end;
  1295.  
  1296.         tkClass:
  1297.         begin
  1298.           { The property is an embedded class }
  1299.  
  1300.           objectProperty := TObject(GetOrdProp(obj, item));
  1301.  
  1302.           if objectProperty is TStrings then
  1303.           begin
  1304.             { The property is a string list }
  1305.  
  1306.             if FTargets.IsPropertyInTargets(obj.ClassName, item^.Name) then
  1307.             begin
  1308.               strings := TStrings(objectProperty);
  1309.  
  1310.               { Save the selected index if the object is either a list or combo box }
  1311.  
  1312.               doTranslate := True;
  1313.               oldIndex := 0;
  1314.               oldSelected := nil;
  1315.               if obj is TCustomListBox then
  1316.               begin
  1317.                 oldIndex := TCustomListBox(obj).ItemIndex;
  1318.  
  1319.                 if (obj is TListBox) and TListBox(obj).MultiSelect then
  1320.                 begin
  1321.                   oldSelected := TList.Create;
  1322.                   for k := 0 to TListBox(obj).Items.Count - 1 do
  1323.                     oldSelected.Add(Pointer(TListBox(obj).Selected[k]));
  1324.                 end;
  1325. {$IFDEF IVWIDE}
  1326.                 if obj is TCheckListBox then
  1327.                 begin
  1328.                   oldSelected := TList.Create;
  1329.                   for k := 0 to TCheckListBox(obj).Items.Count - 1 do
  1330.                     oldSelected.Add(Pointer(TCheckListBox(obj).Checked[k]));
  1331.                 end;
  1332. {$ENDIF}
  1333.               end
  1334.               else if obj is TCustomComboBox then
  1335.                 with TCustomComboBox(obj) do
  1336.                 begin
  1337.                   doTranslate := Visible;
  1338.                   if Visible then
  1339.                     oldIndex := ItemIndex
  1340.                   else
  1341.                     oldIndex := -1
  1342.                 end;
  1343.  
  1344.               if doTranslate and (obj.ClassName <> 'TOutline') then
  1345.               begin
  1346.                 if ivtsPreScanning in FState then
  1347.                 begin
  1348.                   for k := 0 to strings.Count - 1 do
  1349.                     AddTranslation(obj, componentName, item^.Name, strings[i])
  1350.                 end
  1351.                 else
  1352.                   DoTranslateStrings(obj, componentName, item^.Name, strings);
  1353.               end;
  1354.  
  1355.               { Restores the saved item index of the list or combo box }
  1356.  
  1357.               if obj is TCustomListBox then
  1358.               begin
  1359. {$IFDEF IVWIDE}
  1360.                 if obj is TCheckListBox then
  1361.                   for k := 0 to TCheckListBox(obj).Items.Count - 1 do
  1362.                     TCheckListBox(obj).Checked[k] := Boolean(oldSelected[k]);
  1363. {$ENDIF}
  1364.                 if (obj is TListBox) and TListBox(obj).MultiSelect then
  1365.                   for k := 0 to TListBox(obj).Items.Count - 1 do
  1366.                     TListBox(obj).Selected[k] := Boolean(oldSelected[k]);
  1367.  
  1368.                 if oldIndex <> -1 then
  1369.                   TCustomListBox(obj).ItemIndex := oldIndex
  1370.               end
  1371.               else if obj is TCustomComboBox then
  1372.               begin
  1373.                 if oldIndex <> -1 then
  1374.                   TCustomComboBox(obj).ItemIndex := oldIndex;
  1375.               end;
  1376.  
  1377.               oldSelected.Free;
  1378.             end;
  1379.           end
  1380. {$IFDEF WIN32}
  1381.           else if objectProperty is TCollection then
  1382.           begin
  1383.             { The property is a collection }
  1384.  
  1385.             collection := TCollection(objectProperty);
  1386.             for j := 0 to collection.Count - 1 do
  1387.               TranslateObject(collection.Items[j], restrictions);
  1388.           end
  1389. {$ENDIF}
  1390. {$IFDEF IVWIDE}
  1391.           else if objectProperty is TFont then
  1392.           begin
  1393.             { Changes the charset of the font object
  1394.               Do not change TColumnTitle.Font. This is because a bug in VCL that
  1395.               causes the application exception if the charset of TColumnTitle
  1396.               is changed. }
  1397.  
  1398.             if (FDictionary <> nil) and
  1399.               (ivtoChangeFontCharset in FOptions) and
  1400.               not (ivrCharset in restrictions) and
  1401.               (obj.ClassName <> 'TColumnTitle') then
  1402.             begin
  1403.               { Changing of the font character set requires an allocated
  1404.                 windows handle. }
  1405.  
  1406.               if (obj is TWinControl) and not TWinControl(obj).HandleAllocated then
  1407.               begin
  1408.                 doTranslate := (TWinControl(obj).Parent <> nil) and (TWinControl(obj).Parent.HandleAllocated);
  1409.                 if doTranslate then
  1410.                 try
  1411.                   TWinControl(obj).HandleNeeded;
  1412.                 except
  1413.                   doTranslate := False;
  1414.                 end;
  1415.               end
  1416.               else
  1417.                 doTranslate := True;
  1418.  
  1419.               if doTranslate and (TFont(objectProperty).Charset <> Dictionary.LanguageData.Charset) then
  1420.                 TFont(objectProperty).Charset := Dictionary.LanguageData.Charset;
  1421.             end;
  1422.           end
  1423. {$ENDIF}
  1424.           else if objectProperty is TComponent then
  1425.           begin
  1426.             if component <> nil then
  1427.             begin
  1428.               { The property is another component.
  1429.                 Scans the components of the owner component. If finds the same
  1430.                 component does not translate the component. }
  1431.  
  1432.               found := False;
  1433.               ownerComponent := component.Owner;
  1434.               if ownerComponent <> nil then
  1435.               begin
  1436.                 for j := 0 to ownerComponent.ComponentCount - 1 do
  1437.                 begin
  1438.                   if objectProperty = ownerComponent.Components[j] then
  1439.                   begin
  1440.                     found := True;
  1441.                     Break;
  1442.                   end;
  1443.                 end;
  1444.               end;
  1445.  
  1446.               if not found then
  1447.                 TranslateObject(objectProperty, restrictions);
  1448.             end;
  1449.           end
  1450.           else if objectProperty <> nil then
  1451.             TranslateObject(objectProperty, restrictions);
  1452.         end;
  1453.       end;
  1454.     end;
  1455.   finally
  1456.     list.Free;
  1457.   end;
  1458. end;
  1459.  
  1460. procedure TIvTranslator.Translate;
  1461. begin
  1462.   if ivtsBound in FState then
  1463.     TranslateHost
  1464.   else
  1465.   begin
  1466.     if FDictionary = nil then
  1467.     begin
  1468.       { If there is not dictionaris in the application, raises an exception.
  1469.         If the dictionary name was given, finds a dictionary that matches the
  1470.         name.
  1471.         If no name was given or the name was not found uses the first
  1472.         dictionary. }
  1473.  
  1474.       if Dictionaries.Count = 0 then
  1475.       begin
  1476.         { We don't want to throw an exception at design time }
  1477.  
  1478.         if IsDesignTime then
  1479.           Exit;
  1480.         raise EIvMulti.Create(Format(NO_DICTIONARY_C, [Name, Host.Name]));
  1481.       end;
  1482.  
  1483.       if FDictionaryName <> '' then
  1484.         FDictionary := Dictionaries.FindDictionary(FDictionaryName);
  1485.  
  1486.       if FDictionary = nil then
  1487.         FDictionary := Dictionaries[0];
  1488.     end;
  1489.  
  1490.     if FDictionary.IsDesignTime or (not FDictionary.CanBeOpened) or (FTargets.Count = 0) then
  1491.       Exit;
  1492.  
  1493.     if not FDictionary.IsOpen then
  1494.       FDictionary.Open;
  1495.  
  1496. {$IFDEF IVVB}
  1497.     if Host is TComponentOCX then
  1498.     begin
  1499.       FDefaultFontName := TComponentOCX(Host).FontName;
  1500.       FDefaultFontSize := TComponentOCX(Host).FontSize;
  1501.     end
  1502.     else
  1503. {$ENDIF}
  1504.     begin
  1505. {$IFDEF IVWIDE}
  1506.       if Host is TCustomForm then
  1507.       begin
  1508.         FDefaultFontName := TCustomForm(Host).Font.Name;
  1509.         FDefaultFontSize := TCustomForm(Host).Font.Size;
  1510.       end
  1511. {$ELSE}
  1512.       if Host is TForm then
  1513.       begin
  1514.         FDefaultFontName := TForm(Host).Font.Name;
  1515.         FDefaultFontSize := TForm(Host).Font.Size;
  1516.       end
  1517. {$ENDIF}
  1518.       else
  1519.       begin
  1520.         FDefaultFontName := '';
  1521.         FDefaultFontSize := 0;
  1522.       end;
  1523.     end;
  1524.  
  1525.     if FHost = nil then
  1526.       FHost := Owner;
  1527.     TranslateHost;
  1528.     LanguageChanged(True, True);
  1529.     FDictionary.AddTranslator(Self);
  1530.   end;
  1531.  
  1532.   inherited Translate;
  1533. end;
  1534.  
  1535. procedure TIvTranslator.Unbind;
  1536. begin
  1537.   inherited Unbind;
  1538.   FTranslatorTable.Clear;
  1539.   FHost := nil;
  1540. end;
  1541.  
  1542. procedure TIvTranslator.UnbindAndSetNative;
  1543. begin
  1544.   if ivtsBound in FState then
  1545.   begin
  1546.     inherited Unbind;
  1547.     TranslateHost;
  1548.   end;
  1549.   inherited UnbindAndSetNative;
  1550. end;
  1551.  
  1552. procedure TIvTranslator.Open(host: TComponent);
  1553. begin
  1554.   FHost := host;
  1555.   Translate;
  1556. end;
  1557.  
  1558. procedure TIvTranslator.Close;
  1559. var
  1560.   oldLocale: Integer;
  1561.   oldOptions: TIvDictionaryOptions;
  1562. begin
  1563.   if FDictionary <> nil then
  1564.   begin
  1565.     oldOptions := FDictionary.Options;
  1566.     oldLocale := FDictionary.Locale;
  1567.     FDictionary.Options := FDictionary.Options - [ivdoAutoTranslate];
  1568.     FDictionary.Language := 0;
  1569.     Translate;
  1570.     FDictionary.Locale := oldLocale;
  1571.     FDictionary.Options := oldOptions;
  1572.   end;
  1573.   Unbind;
  1574. end;
  1575.  
  1576. function TIvTranslator.GetNativePropertyValue(
  1577.   component: TObject;
  1578.   const propertyName: String): String;
  1579. var
  1580.   i: Integer;
  1581. begin
  1582.   for i := 0 to FTranslatorTable.Count - 1 do
  1583.   begin
  1584.     if (FTranslatorTable.Items[i].Obj = component) and
  1585.       (CompareText(FTranslatorTable.Items[i].PropertyName, propertyName) = 0) then
  1586.     begin
  1587.       Result := FTranslatorTable.Items[i].Native;
  1588.       Exit;
  1589.     end;
  1590.   end;
  1591.  
  1592.   raise Exception.Create('Property has not been translated');
  1593. end;
  1594.  
  1595. function TIvTranslator.GetNativePropertyValueEx(
  1596.   component: TObject;
  1597.   const propertyName, currentValue: String): String;
  1598. var
  1599.   i: Integer;
  1600. begin
  1601.   for i := 0 to FTranslatorTable.Count - 1 do
  1602.   begin
  1603.     if (FTranslatorTable.Items[i].Obj = component) and
  1604.       (CompareText(FTranslatorTable.Items[i].PropertyName, propertyName) = 0) and
  1605.       (CompareText(FTranslatorTable.Items[i].Current, currentValue) = 0) then
  1606.     begin
  1607.       Result := FTranslatorTable.Items[i].Native;
  1608.       Exit;
  1609.     end;
  1610.   end;
  1611.  
  1612.   raise Exception.Create('Property has not been translated');
  1613. end;
  1614.  
  1615. procedure TIvTranslator.LanguageChanged(languageChanged, localeChanged: Boolean);
  1616. begin
  1617.   if (ivtsBound in FState) and (ivtoAutoTranslate in FOptions) then
  1618.     Translate;
  1619.  
  1620.   if languageChanged and Assigned(FOnLanguageChange) then
  1621.     FOnLanguageChange(Self);
  1622.  
  1623.   if localeChanged and Assigned(FOnLocaleChange) then
  1624.     FOnLocaleChange(Self);
  1625. end;
  1626.  
  1627. procedure TIvTranslator.CheckHostFont;
  1628. {$IFDEF IVANSI}
  1629. const
  1630.   MICROSOFT_SANS_SERIF_FONT_C = 'Microsoft Sans Serif';
  1631.   MS_SANS_SERIF_FONT_C = 'MS Sans Serif';
  1632.   MS_SYSTEM_FONT_C = 'System';
  1633.   ARIAL_FONT_C = 'Arial';
  1634. {$ENDIF}
  1635. var
  1636.   fontSize: Integer;
  1637.   fontName: String;
  1638. {$IFDEF IVANSI}
  1639.   names: TStringList;
  1640. {$ENDIF}
  1641.  
  1642. {$IFDEF IVANSI}
  1643.   function DoesListContainFontname(const value: String): Boolean;
  1644.   var
  1645.     i: Integer;
  1646.   begin
  1647.     Result := False;
  1648.     for i := 0 to names.Count - 1 do
  1649.       if CompareText(names[i], value) = 0 then
  1650.       begin
  1651.         Result := True;
  1652.         Exit;
  1653.       end;
  1654.   end;
  1655. {$ENDIF}
  1656.  
  1657. begin
  1658.   { Checks first the custom font name of the current language. If it is
  1659.     not given the original font name of the host is used. }
  1660.  
  1661.   fontName := FDictionary.LanguageData.FontName;
  1662.   if fontName = '' then
  1663.     fontName := FDefaultFontName;
  1664.  
  1665.   fontSize := FDictionary.LanguageData.FontSize;
  1666.   if fontSize = 0 then
  1667.     fontSize := FDefaultFontSize;
  1668.  
  1669. {$IFDEF IVANSI}
  1670.   { Delphi 2.0 and C++Builder 1.0 do not have support for font character sets.
  1671.     That's why the font to be used must support the current character set-
  1672.     If it do not match the following fonts are tried:
  1673.     'Microsoft Sanf Serif' -> 'MS Sanf Serif' -> 'System' -> 'Arial' in this order.
  1674.     If none of above matches the first font that support the character set is
  1675.     used. }
  1676.  
  1677.   names := TStringList.Create;
  1678.   try
  1679.     IvGetFontNames(
  1680.       [IvCodeToCharset(IvLangIdToCharset(FDictionary.Locale))],
  1681.       names);
  1682.  
  1683.     if DoesListContainFontname(fontName) then
  1684.       fontName := fontName
  1685.     else if DoesListContainFontname(FDefaultFontName) then
  1686.       fontName := FDefaultFontName
  1687.     else if DoesListContainFontname(MICROSOFT_SANS_SERIF_FONT_C) then
  1688.       fontName := MICROSOFT_SANS_SERIF_FONT_C
  1689.     else if DoesListContainFontname(MS_SANS_SERIF_FONT_C) then
  1690.       fontName := MS_SANS_SERIF_FONT_C
  1691.     else if DoesListContainFontname(MS_SYSTEM_FONT_C) then
  1692.       fontName := MS_SYSTEM_FONT_C
  1693.     else if DoesListContainFontname(ARIAL_FONT_C) then
  1694.       fontName := ARIAL_FONT_C
  1695.     else if names.Count > 0 then
  1696.       fontName := names[0];
  1697.   finally
  1698.     names.Free;
  1699.   end;
  1700. {$ENDIF}
  1701.  
  1702.   { Sets the new font name, size and charset for the host form }
  1703.  
  1704. {$IFDEF IVWIDE}
  1705.   if (Host is TCustomForm) then
  1706.   begin
  1707.     with TCustomForm(Host) do
  1708.     begin
  1709.       if HandleAllocated then
  1710.       begin
  1711.         if fontName <> Font.Name then
  1712.           Font.Name := fontName;
  1713.  
  1714.         if fontSize <> Font.Size then
  1715.           Font.Size := fontSize;
  1716.  
  1717.         {if (FDictionary.LanguageData.Charset <> Font.Charset) and (FDictionary.LanguageData.Charset <> ANSI_CHARSET) then}
  1718.         if FDictionary.LanguageData.Charset <> Font.Charset then
  1719.           Font.Charset := FDictionary.LanguageData.Charset;
  1720.       end;
  1721.     end;
  1722.   end;
  1723. {$ELSE}
  1724.   if (Host is TForm) then
  1725.   begin
  1726.     with TForm(Host) do
  1727.     begin
  1728.       if HandleAllocated then
  1729.       begin
  1730.         if fontName <> Font.Name then
  1731.           Font.Name := fontName;
  1732.  
  1733.         if fontSize <> Font.Size then
  1734.           Font.Size := fontSize;
  1735.       end;
  1736.     end;
  1737.   end;
  1738. {$ENDIF}
  1739. end;
  1740.  
  1741. procedure TIvTranslator.TranslateHost;
  1742. var
  1743.   mdi: Boolean;
  1744. {$IFDEF IVPRO32}
  1745.   oldState: TIvTranslatorState;
  1746. {$ENDIF}
  1747. {$IFDEF IVVB}
  1748.   vbFormHost: boolean;
  1749. {$ENDIF}
  1750.  
  1751.   procedure QuickSort(left, right: Integer);
  1752.   var
  1753.     i, j: Integer;
  1754.     p: String;
  1755.     translation: TIvTranslation;
  1756.   begin
  1757.     i := left;
  1758.     j := right;
  1759.     p := TIvTranslation(FTranslations[(left + right) shr 1]).Key;
  1760.  
  1761.     repeat
  1762.       while SysUtils.CompareStr(TIvTranslation(FTranslations[i]).Key, p) < 0 do
  1763.         Inc(i);
  1764.       while SysUtils.CompareStr(TIvTranslation(FTranslations[j]).Key, p) > 0 do
  1765.         Dec(j);
  1766.  
  1767.       if i <= j then
  1768.       begin
  1769.         translation := FTranslations[i];
  1770.         FTranslations[i] := FTranslations[j];
  1771.         FTranslations[j] := translation;
  1772.         Inc(i);
  1773.         Dec(j);
  1774.       end;
  1775.     until i > j;
  1776.  
  1777.     if left < j then
  1778.       QuickSort(left, j);
  1779.  
  1780.     if i < right then
  1781.       QuickSort(i, right);
  1782.   end;
  1783.  
  1784. begin
  1785.   inherited TranslateHost;
  1786.  
  1787.   if Host is TForm then
  1788.     mdi := TForm(Host).FormStyle = fsMDIChild
  1789.   else
  1790.     mdi := False;
  1791.  
  1792.   { If the dictionary uses multiple translation, prescans the from to get the
  1793.     translations. }
  1794.  
  1795.   if FDictionary <> nil then
  1796.   begin
  1797.     if Dictionary.GetTranslationMode = ivtmMultiple then
  1798.     begin
  1799.       FState := FState + [ivtsPreScanning];
  1800.       try
  1801.         ClearTranslations;
  1802.         TranslateSubComponent(Host, []);
  1803. {$IFDEF WIN32}
  1804.         if ivtoTranslateSystemMenu in FOptions then
  1805.           TranslateSystemMenu(GetSystemMenuWinHandle, mdi);
  1806. {$ENDIF}
  1807.         if FTranslations.Count > 0 then
  1808.         begin
  1809.           QuickSort(0, FTranslations.Count - 1);
  1810.           Dictionary.TranslateStrings(FTranslations);
  1811.          end;
  1812.       finally
  1813.         FState := FState - [ivtsPreScanning];
  1814.       end;
  1815.     end;
  1816.  
  1817. {$IFDEF IVPRO32}
  1818.     if Dictionary.LanguageData.CharsetType = ivcsBiDirectional then
  1819.       FReadingOrder := ivroRightToLeft
  1820.     else
  1821.       FReadingOrder := ivroLeftToRight;
  1822. {$ENDIF}
  1823.   end;
  1824.  
  1825.   if Assigned(FOnBeforeTranslate) then
  1826.     FOnBeforeTranslate(Self);
  1827.  
  1828. {$IFDEF IVVB}
  1829.   vbFormHost := ((Host is TComponentOCX) and (TComponentOCX(Host).IsFormObject));
  1830. {$ENDIF}
  1831.  
  1832.   { Check the host font }
  1833.  
  1834.   if (FDictionary <> nil) and (ivtoCheckFont in FOptions) and (
  1835. {$IFDEF IVVB}
  1836.     (vbFormHost = TRUE) or
  1837. {$ENDIF}
  1838. {$IFDEF IVVB}
  1839.     ((Host is TComponentOCX) and (TComponentOCX(Host).IsFormObject)) or
  1840. {$ENDIF}
  1841. {$IFDEF IVWIDE}
  1842.     (Host is TCustomForm)) then
  1843. {$ELSE}
  1844.     (Host is TForm)) then
  1845. {$ENDIF}
  1846.   begin
  1847.     CheckHostFont;
  1848.   end;
  1849.  
  1850.   { Checks that the font of the host component supports the current language.
  1851.     Translates the host component }
  1852.  
  1853.   TranslateSubComponent(Host, []);
  1854.  
  1855. {$IFDEF WIN32}
  1856.   if ivtoTranslateSystemMenu in FOptions then
  1857.     TranslateSystemMenu(GetSystemMenuWinHandle, mdi);
  1858. {$ENDIF}
  1859.  
  1860. {$IFDEF IVPRO}
  1861.   { Scales the host component if needed }
  1862.  
  1863.   if (FDictionary <> nil) and (ivtoScaleMultiByte in FOptions) and (FMultiByteScale <> 100) and (
  1864. {$IFDEF IVVB}
  1865.     (vbFormHost = TRUE) or
  1866. {$ENDIF}
  1867.     (Host is TWinControl)) then
  1868.   begin
  1869.     if FDictionary.LanguageData.CharsetType = ivcsMultiByte then
  1870.     begin
  1871.       if not (ivtsScaled in FState) then
  1872.       begin
  1873.         if (Host is TForm) and TForm(Host).AutoScroll then
  1874.           raise EIvMulti.Create('Cannot scale a form (' + TForm(Host).Name + ') that has AutoScroll property set True');
  1875.  
  1876. {$IFDEF IVVB}
  1877.         if vbFormHost then
  1878.           TComponentOCX(Host).ScaleBy(FMultiByteScale, 100)
  1879.         else
  1880. {$ENDIF}
  1881.           TWinControl(Host).ScaleBy(FMultiByteScale, 100);
  1882.  
  1883.         FState := FState + [ivtsScaled];
  1884.         if Assigned(FOnScale) then
  1885.           FOnScale(Self, Host);
  1886.       end;
  1887.     end
  1888.     else
  1889.     begin
  1890.       if(ivtsScaled in FState) then
  1891.       begin
  1892. {$IFDEF IVVB}
  1893.         if vbFormHost then
  1894.           TComponentOCX(Host).ScaleBy(100, FMultiByteScale)
  1895.         else
  1896. {$ENDIF}
  1897.           TWinControl(Host).ScaleBy(100, FMultiByteScale);
  1898.  
  1899.         FState := FState - [ivtsScaled];
  1900.         if Assigned(FOnScale) then
  1901.           FOnScale(Self, Host);
  1902.       end;
  1903.     end;
  1904.   end;
  1905. {$ENDIF}
  1906.  
  1907. {$IFDEF IVPRO32}
  1908.   { Sets the reading order of the host component if needed }
  1909.  
  1910.   if (ivtoMirrorBiDirectional in FOptions) then
  1911.   begin
  1912.     oldState := FState;
  1913.  
  1914.     if FReadingOrder <> FNativeLayout then
  1915.       FState := FState + [ivtsMirrored]
  1916.     else
  1917.       FState := FState - [ivtsMirrored];
  1918.  
  1919.     { If the bidirectional state has been changed redraws the menu }
  1920.  
  1921.     if (Host is TWinControl) and (oldState <> FState) then
  1922.     begin
  1923.       SetWindowPos(
  1924.         TWinControl(Host).Handle,
  1925.         0,
  1926.         0,
  1927.         0,
  1928.         0,
  1929.         0,
  1930.         SWP_NOMOVE or SWP_NOSIZE or SWP_DRAWFRAME);
  1931.     end;
  1932.   end;
  1933. {$ENDIF}
  1934.  
  1935.   if Assigned(FOnAfterTranslate) then
  1936.     FOnAfterTranslate(Self);
  1937. end;
  1938.  
  1939. function TIvTranslator.GetBidirectionalState(control: TControl; allocNew: Boolean): TIvBidirectionalState;
  1940. {$IFDEF IVPRO32}
  1941. var
  1942.   i: Integer;
  1943.  {$IFDEF IVVB}
  1944.   componentOCX: TComponentOCX;
  1945.  {$ENDIF}
  1946. {$ENDIF}
  1947. begin
  1948. {$IFDEF IVPRO32}
  1949.   {$IFDEF IVVB}
  1950.   componentOCX := nil;
  1951.   if TObject(control) is TComponentOCX then
  1952.     componentOCX := TComponentOCX(control);
  1953.   {$ENDIF}
  1954.  
  1955.   for i := 0 to FBidirectionalStates.Count - 1 do
  1956.   begin
  1957.     Result := FBidirectionalStates[i];
  1958.  
  1959.   {$IFDEF IVVB}
  1960.     if (componentOCX <> nil) then begin
  1961.       if (Result.Control = TControl(componentOCX.VBDispatch)) then
  1962.         Exit
  1963.     end
  1964.     else
  1965.   {$ENDIF}
  1966.       if Result.Control = control then
  1967.         Exit;
  1968.   end;
  1969.  
  1970.   if allocNew then
  1971.   begin
  1972.     Result := TIvBidirectionalState.Create(control);
  1973. {$IFDEF IVVB}
  1974.     { OCX: Dispatch is permanent object in VB instead of TComponentOCX }
  1975.     if componentOCX <> nil then
  1976.       Result.Control := TControl(componentOCX.VBDispatch);
  1977. {$ENDIF}
  1978.     FBidirectionalStates.Add(Result);
  1979.   end
  1980.   else
  1981.     Result := nil;
  1982. {$ELSE}
  1983.   Result := nil;
  1984. {$ENDIF}
  1985. end;
  1986.  
  1987. procedure TIvTranslator.UpdateControl(control: TControl);
  1988. {$IFDEF IVPRO32}
  1989. var
  1990.   i: Integer;
  1991.   state: TIvBidirectionalState;
  1992. {$ENDIF}
  1993. begin
  1994. {$IFDEF IVPRO32}
  1995.   if not (ivtsMirrored in FState) or (control.Parent = nil) or (control = Host) then
  1996.     Exit;
  1997.  
  1998.   for i := 0 to FBidirectionalStates.Count - 1 do
  1999.   begin
  2000.     state := TIvBidirectionalState(FBidirectionalStates[i]);
  2001.     if (state.Control = control) and state.Flipped  then
  2002.     begin
  2003.       control.Left := control.Parent.ClientWidth - control.Width - state.OriginalLeft;
  2004.       Exit;
  2005.     end;
  2006.   end;
  2007. {$ENDIF}
  2008. end;
  2009.  
  2010. procedure TIvTranslator.UpdateControls;
  2011. {$IFDEF IVPRO32}
  2012. var
  2013.   i, left: Integer;
  2014. {$ENDIF}
  2015. begin
  2016. {$IFDEF IVPRO32}
  2017.   if not (ivtsMirrored in FState) then
  2018.     Exit;
  2019.  
  2020.   for i := 0 to FBidirectionalStates.Count - 1 do
  2021.     with TIvBidirectionalState(FBidirectionalStates[i]) do
  2022.     begin
  2023.       if (Control = Host) or (Control.Parent = nil) then
  2024.         Continue;
  2025.         
  2026.       left := Control.Parent.ClientWidth - Control.Width - OriginalLeft;
  2027.       if Flipped and (left <> Control.Left) then
  2028.         Control.Left := left;
  2029.     end;
  2030. {$ENDIF}
  2031. end;
  2032.  
  2033. procedure TIvTranslator.FlipControl(control: TControl; state: TIvBidirectionalState);
  2034. begin
  2035. {$IFDEF IVPRO32}
  2036.   if Modules.FlipControl(Self, control, state) then
  2037.     Exit;
  2038.  
  2039.   if not state.Flipped then
  2040.   begin
  2041.     state.Flipped := True;
  2042.     state.ReadingOrder := ivroRightToLeft;
  2043.     control.Left := control.Parent.ClientWidth - control.Width - control.Left;
  2044.   end;
  2045. {$ENDIF}
  2046. end;
  2047.  
  2048. procedure TIvTranslator.UnFlipControl(control: TControl; state: TIvBidirectionalState);
  2049. begin
  2050. {$IFDEF IVPRO32}
  2051.   if Modules.UnFlipControl(Self, control, state) then
  2052.     Exit;
  2053.  
  2054.   if state.Flipped then
  2055.   begin
  2056.     state.Flipped := False;
  2057.     state.ReadingOrder := ivroLeftToRight;
  2058.     control.Left := state.OriginalLeft;
  2059.   end;
  2060. {$ENDIF}
  2061. end;
  2062.  
  2063. procedure TIvTranslator.TranslateComponent(component: TComponent);
  2064. var
  2065.   str: String;
  2066. begin
  2067.   Modules.TranslateComponent(Self, component);
  2068.  
  2069.   if (component is TCustomMemo) then
  2070.   begin
  2071.     if FTargets.IsPropertyInTargets(component.ClassName, 'Text') then
  2072.       with TCustomMemo(component) do
  2073.       begin
  2074.         str := Text;
  2075.         while (str <> '') and ((str[Length(str)] = #10) or (str[Length(str)] = #13)) do
  2076.           Delete(str, Length(str), 1);
  2077.  
  2078.         if ivtsPreScanning in FState then
  2079.           AddTranslation(
  2080.             component,
  2081.             component.Name,
  2082.             'Text',
  2083.             str)
  2084.         else
  2085.           Text := DoTranslateContextString(
  2086.             component,
  2087.             component.Name,
  2088.             'Text',
  2089.             str);
  2090.       end;
  2091.   end;
  2092. end;
  2093.  
  2094. procedure TIvTranslator.TranslateSubComponent(
  2095.   component: TComponent;
  2096.   parentRestrictions: TIvRestrictions);
  2097. var
  2098.   i: Integer;
  2099.   restrictions, childRestrictions: TIvRestrictions;
  2100.   subComponent: TComponent;
  2101. {$IFDEF IVPRO32}
  2102.   changeReadingOrder: Boolean;
  2103.   bidirectionalState: TIvBidirectionalState;
  2104. {$ENDIF}
  2105. begin
  2106.   if ivtsPreScanning in FState then
  2107.     childRestrictions := []
  2108.   else
  2109.   begin
  2110.     { Gets the restriction for the component }
  2111.  
  2112.     restrictions := parentRestrictions;
  2113.     if Assigned(FOnRestrictComponent) then
  2114.       FOnRestrictComponent(Self, component, restrictions);
  2115.  
  2116.     { Translates the sub components }
  2117.  
  2118.     if ivrThisComponentOnly in restrictions then
  2119.       childRestrictions := []
  2120.     else
  2121.       childRestrictions := restrictions;
  2122.   end;
  2123.  
  2124.   for i := 0 to component.ComponentCount - 1 do
  2125.   begin
  2126.     subComponent := component.Components[i];
  2127. {$IFDEF IVWIDE}
  2128.     if not (subComponent is TCustomForm) then
  2129. {$ELSE}
  2130.     if not (subComponent is TForm) then
  2131. {$ENDIF}
  2132.       TranslateSubComponent(subComponent, childRestrictions);
  2133.   end;
  2134.  
  2135.   { Performs build in translation }
  2136.  
  2137.   if not (ivrBuildInTranslation in restrictions) then
  2138.     TranslateObject(component, restrictions);
  2139.  
  2140.   { Performs custom translation }
  2141.  
  2142.   if not (ivrCustomTranslation in restrictions) then
  2143.     TranslateComponent(component);
  2144.  
  2145.   if ivtsPreScanning in FState then
  2146.     Exit;
  2147.  
  2148. {$IFDEF IVPRO32}
  2149.   if ivtoMirrorBidirectional in FOptions then
  2150.   begin
  2151.     { Changes the component reading order and flips/unflips it }
  2152.  
  2153.     changeReadingOrder := True;
  2154.     if (component is TControl)
  2155.   {$IFDEF IVVB}
  2156.       or (component is TComponentOCX)
  2157.   {$ENDIF}
  2158.     then
  2159.     begin
  2160.       bidirectionalState := GetBidirectionalState(TControl(component), True);
  2161.       changeReadingOrder := bidirectionalState.ReadingOrder <> FReadingOrder;
  2162.  
  2163.       { Flips/unflips the control }
  2164.  
  2165.       if not (ivrFlip in restrictions) then
  2166.       begin
  2167.         if component = Host then
  2168.           bidirectionalState.ReadingOrder := FReadingOrder
  2169.         else
  2170.         begin
  2171.           if FReadingOrder <> FNativeLayout then
  2172.             FlipControl(TControl(component), bidirectionalState)
  2173.           else
  2174.             UnFlipControl(TControl(component), bidirectionalState);
  2175.         end;
  2176.       end;
  2177.    end;
  2178.  
  2179.    { Sets the component reading order }
  2180.  
  2181.     if not (ivrReadingOrder in restrictions) and changeReadingOrder then
  2182.       ChangeComponentReadingOrder(component);
  2183.   end;
  2184. {$ENDIF}
  2185.  
  2186.   if Assigned(FOnTranslate) then
  2187.     FOnTranslate(Self, component);
  2188.  
  2189.   if component is TControl then
  2190.     TControl(component).Invalidate;
  2191. end;
  2192.  
  2193. procedure TIvTranslator.ChangeComponentReadingOrder(component: TComponent);
  2194. {$IFDEF IVPRO32}
  2195.   {$IFNDEF IVBIDI}
  2196. var
  2197.   style, newStyle: Integer;
  2198.   info: TMenuItemInfo;
  2199.   buffer: array[0..255] of Char;
  2200.   {$ENDIF}
  2201. {$ENDIF}
  2202. begin
  2203. {$IFDEF IVPRO32}
  2204.   if Modules.ChangeComponentReadingOrder(Self, component) then
  2205.     Exit;
  2206.  
  2207.   {$IFNDEF IVBIDI}
  2208.   { ML can right align some standard VCL's components }
  2209.  
  2210.   if component is TLabel then
  2211.     with TLabel(component) do
  2212.     begin
  2213.       if Alignment = taLeftJustify then
  2214.         Alignment := taRightJustify
  2215.       else if Alignment = taRightJustify then
  2216.         Alignment := taLeftJustify;
  2217.     end
  2218.   else if component is TRadioButton then
  2219.     with TRadioButton(component) do
  2220.     begin
  2221.       if Alignment = taLeftJustify then
  2222.         Alignment := taRightJustify
  2223.       else if Alignment = taRightJustify then
  2224.         Alignment := taLeftJustify;
  2225.     end
  2226.   else if component is TCheckBox then
  2227.     with TCheckBox(component) do
  2228.     begin
  2229.       if Alignment = taLeftJustify then
  2230.         Alignment := taRightJustify
  2231.       else if Alignment = taRightJustify then
  2232.         Alignment := taLeftJustify;
  2233.     end
  2234.   else if component is TMemo then
  2235.     with TMemo(component) do
  2236.     begin
  2237.       if Alignment = taLeftJustify then
  2238.         Alignment := taRightJustify
  2239.       else if Alignment = taRightJustify then
  2240.         Alignment := taLeftJustify;
  2241.     end
  2242.   else if component is TMenu then
  2243.   begin
  2244.     info.cbSize := SizeOf(info);
  2245.     info.fMask := MIIM_TYPE;
  2246.     info.dwTypeData := buffer;
  2247.     info.cch := Sizeof(buffer);
  2248.     GetMenuItemInfo(TMenu(component).Handle, 0, True, info);
  2249.     if ReadingOrder = ivroRightToLeft then
  2250.       style := info.fType or MFT_RIGHTORDER or MFT_RIGHTJUSTIFY
  2251.     else
  2252.       style := info.fType and not (MFT_RIGHTORDER or MFT_RIGHTJUSTIFY);
  2253.     if style <> Integer(info.fType) then
  2254.     begin
  2255.       info.fType := style;
  2256.       SetMenuItemInfo(TMenu(component).Handle, 0, True, info);
  2257.     end;
  2258.   end;
  2259.  
  2260.   { Changes the Window style of a window component }
  2261.  
  2262.   if component is TWinControl then
  2263.   begin
  2264.     { Extended style }
  2265.  
  2266.     style := GetWindowLong(TWinControl(component).Handle, GWL_EXSTYLE);
  2267.     if readingOrder = ivroRightToLeft then
  2268.       newStyle := style or WS_EX_RIGHT or WS_EX_LEFTSCROLLBAR or WS_EX_RTLREADING
  2269.     else
  2270.       newStyle := style and not (WS_EX_RIGHT or WS_EX_LEFTSCROLLBAR or WS_EX_RTLREADING);
  2271.     if newStyle <> style then
  2272.       SetWindowLong(TWinControl(component).Handle, GWL_EXSTYLE, newStyle);
  2273.  
  2274.     { Normal style }
  2275.  
  2276.     style := GetWindowLong(TWinControl(component).Handle, GWL_STYLE);
  2277.     newStyle := style;
  2278.     if (component is TCustomCheckBox) or (component is TRadioButton) then
  2279.     begin
  2280.       if readingOrder = ivroRightToLeft then
  2281.         newStyle := newStyle or BS_RIGHT or BS_RIGHTBUTTON
  2282.       else
  2283.         newStyle := newStyle and not (BS_RIGHT or BS_RIGHTBUTTON);
  2284.     end
  2285.     else if component is TCustomEdit then
  2286.     begin
  2287.       if readingOrder = ivroRightToLeft then
  2288.         newStyle := newStyle or ES_RIGHT
  2289.       else
  2290.         newStyle := newStyle and not ES_RIGHT;
  2291.     end;
  2292.     if newStyle <> style then
  2293.       SetWindowLong(TWinControl(component).Handle, GWL_STYLE, newStyle);
  2294.   end;
  2295.   {$ENDIF}
  2296. {$ENDIF}
  2297. end;
  2298.  
  2299. procedure TIvTranslator.DetectTargets(
  2300.   targets: TIvTargetProperties;
  2301.   prompt: Boolean);
  2302. var
  2303.   i: Integer;
  2304. begin
  2305.   { Scans all the sub componets adding the targets they need }
  2306.  
  2307.   AddObjectTargets(Host, targets, prompt);
  2308.   for i := 0 to Host.ComponentCount - 1 do
  2309.     AddObjectTargets(Host.Components[i], targets, prompt);
  2310. end;
  2311.  
  2312. procedure TIvTranslator.AddObjectTargets(
  2313.   obj: TObject;
  2314.   targets: TIvTargetProperties;
  2315.   prompt: Boolean);
  2316. const
  2317.   DEFAULT_TARGET_COUNT_C = 6;
  2318.   DEFAULT_TARGETS_C: array[0..DEFAULT_TARGET_COUNT_C - 1] of String =
  2319.   (
  2320.     'Caption',
  2321.     'Hint',
  2322.     'Items',
  2323.     'Lines',
  2324.     'Hints',
  2325.     'Tabs'
  2326.   );
  2327. var
  2328.   i, j: Integer;
  2329.   found: Boolean;
  2330.   item: PPropInfo;
  2331.   list: TIvPropInfoList;
  2332.   objectProperty: TObject;
  2333.   availableTargets: TIvTargetProperties;
  2334.   fileName, line, objectName, propertyName: String;
  2335.   f: TextFile;
  2336.   parser: TIvStringParser;
  2337.   component, ownerComponent: TComponent;
  2338.  
  2339.   procedure Add(obj: TObject; name: String; check: Boolean);
  2340.   var
  2341.     id: Integer;
  2342. {$IFNDEF WIN32}
  2343.     buffer: array[0..255] of Char;
  2344. {$ENDIF}
  2345.   begin
  2346.     { If the property has been excluded it is not added }
  2347.  
  2348.     if targets.IsPropertyExcluded(obj.ClassName, name) then
  2349.       Exit;
  2350.  
  2351.     { The property is added if it is included into the avaiable targets and
  2352.       it is not included to the targets }
  2353.  
  2354.     if (not check or (check and (availableTargets <> nil) and availableTargets.IsPropertyInTargets(obj.ClassName, name))) and
  2355.       not targets.IsPropertyInTargets(obj.ClassName, name) then
  2356.     begin
  2357.       if prompt then
  2358.         id := MessageBox(
  2359.           0,
  2360. {$IFDEF WIN32}PChar({$ELSE}StrPCopy(buffer,{$ENDIF}
  2361.           'The translator can translate the ' + obj.ClassName + '.' + name +
  2362.             ' property and all the other "' + name + '" properties on the "' + Host.Name + '" form.'#13#10 +
  2363.             'Do you want to add them to the targets?'),
  2364.           'A new target property found',
  2365.           MB_YESNOCANCEL)
  2366.       else
  2367.         id := IDYES;
  2368.  
  2369.       case id of
  2370.         IDYES: targets.Add(TIvTargetProperty.Create('', name, ivttInclude));
  2371.         IDNO: targets.Add(TIvTargetProperty.Create(obj.ClassName, name, ivttExclude));
  2372.       end;
  2373.     end;
  2374.   end;
  2375.  
  2376. begin
  2377.   { Do not scan dictionary or translator objects }
  2378.  
  2379.   if (obj is TIvDictionary) or (obj is TIvTranslator) then
  2380.     Exit;
  2381.  
  2382.   { Adds the complex targets }
  2383.  
  2384.   if (obj.ClassName = 'TStringGrid') then
  2385.     Add(obj, 'Cells', False);
  2386.   if (obj.ClassName = 'TOutline') then
  2387.     Add(obj, 'Lines', False);
  2388. {$IFDEF WIN32}
  2389.   if (obj.ClassName = 'TTreeView') then
  2390.     Add(obj, 'Items', False);
  2391.   if (obj.ClassName = 'TListView') then
  2392.   begin
  2393.     Add(obj, 'Items', False);
  2394.     Add(obj, 'Caption', False);
  2395.   end;
  2396. {$ENDIF}
  2397.  
  2398.   { Gets the available targets }
  2399.  
  2400.   fileName := GetMLRegistryValue(ROOT_DIR_C, '.') + '\' + TARGETS_C;
  2401.   availableTargets := TIvTargetProperties.Create;
  2402.   try
  2403.     { Reads the available targets from the TARGET.TXT file }
  2404.  
  2405.     if (fileName <> '') and FileExists(fileName) then
  2406.     begin
  2407.       parser := TIvStringParser.CreateValue('', ',');
  2408.       try
  2409.         AssignFile(f, fileName);
  2410.         Reset(f);
  2411.         try
  2412.           while not Eof(f) do
  2413.           begin
  2414.             Readln(f, line);
  2415.             if (line = '') or (line[1] = ';') or (line[1] = ':') then
  2416.               Continue;
  2417.  
  2418.             parser.Value := line;
  2419.             objectName := parser.GetString;
  2420.             propertyName := parser.GetString;
  2421.             availableTargets.Add(TIvTargetProperty.Create(objectName, propertyName, ivttInclude));
  2422.           end;
  2423.         finally
  2424.           CloseFile(f);
  2425.         end;
  2426.       finally
  2427.         parser.Free;
  2428.       end;
  2429.     end;
  2430.  
  2431.     { If the file does not exists or is null uses the default targets }
  2432.  
  2433.     if availableTargets.Count = 0 then
  2434.     begin
  2435.       for i := 0 to DEFAULT_TARGET_COUNT_C - 1 do
  2436.         availableTargets.Add(TIvTargetProperty.Create('', DEFAULT_TARGETS_C[i], ivttInclude));
  2437.     end;
  2438.  
  2439.     { If available targets contains items, scan the componet to find those
  2440.       targets. }
  2441.  
  2442.     list := TIvPropInfoList.Create(
  2443.       obj,
  2444.       [tkClass, tkString{$IFDEF WIN32}, tkLString{$ENDIF}]);
  2445.  
  2446.     try
  2447.       for i := 0 to list.Count - 1 do
  2448.       begin
  2449.         { Checks if the property is read only.
  2450.           In that case it is not translated }
  2451.  
  2452.         item := list.Items[i];
  2453.         if (item^.SetProc = nil) or (item^.GetProc = nil) then
  2454.           Continue;
  2455.  
  2456.         case item^.PropType^.Kind of
  2457. {$IFDEF WIN32}
  2458.           tkLString,
  2459. {$ENDIF}
  2460.           tkString:
  2461.           begin
  2462.             Add(obj, item^.Name, True);
  2463.           end;
  2464.  
  2465.           tkClass:
  2466.           begin
  2467.             { The property is an embedded class }
  2468.  
  2469.             try
  2470.               objectProperty := TObject(GetOrdProp(obj, item));
  2471.  
  2472.               if availableTargets.IsObjectInTargets(objectProperty.ClassName) then
  2473.               begin
  2474.                 if not targets.IsObjectInTargets(objectProperty.ClassName) then
  2475.                   targets.Add(TIvTargetProperty.Create(objectProperty.ClassName, '', ivttInclude));
  2476.               end
  2477.               else if objectProperty is TStrings then
  2478.                 Add(obj, item^.Name, True)
  2479. {$IFDEF WIN32}
  2480.               else if objectProperty is TCollection then
  2481.               begin
  2482.                 { The property is a collection }
  2483.  
  2484.                 with TCollection(objectProperty) do
  2485.                   for j := 0 to Count - 1 do
  2486.                     AddObjectTargets(Items[j], targets, prompt);
  2487.               end
  2488. {$ENDIF}
  2489.               else if objectProperty is TComponent then
  2490.               begin
  2491.                 if obj is TComponent then
  2492.                 begin
  2493.                   component := TComponent(obj);
  2494.  
  2495.                   { The property is another component.
  2496.                     Scans the components of the owner component to avoid
  2497.                     circular references. }
  2498.  
  2499.                   found := False;
  2500.                   ownerComponent := component.Owner;
  2501.                   if ownerComponent <> nil then
  2502.                   begin
  2503.                     for j := 0 to ownerComponent.ComponentCount - 1 do
  2504.                     begin
  2505.                       if objectProperty = ownerComponent.Components[j] then
  2506.                       begin
  2507.                         found := True;
  2508.                         Break;
  2509.                       end;
  2510.                     end;
  2511.                   end;
  2512.  
  2513.                   if not found then
  2514.                     AddObjectTargets(objectProperty, targets, prompt);
  2515.                 end;
  2516.               end
  2517.               else if objectProperty <> nil then
  2518.                 AddObjectTargets(objectProperty, targets, prompt);
  2519.             except
  2520.               { The detection of this property failed. The most probably
  2521.                 reason for this is a bad desing of the component. For example
  2522.                 the TTable component raises an exaception if the MasterSource
  2523.                 property is read in the early state of the component. }
  2524.             end;
  2525.           end;
  2526.         end;
  2527.       end;
  2528.     finally
  2529.       list.Free;
  2530.     end;
  2531.   finally
  2532.     availableTargets.Free;
  2533.   end;
  2534. end;
  2535.  
  2536. procedure TIvTranslator.Loaded;
  2537. begin
  2538.   { Translates the host if auto translate is on and the dictionary is given. }
  2539.  
  2540.   if not IsDesignTime and
  2541.     (ivtoAutoOpen in FOptions) and
  2542.     ((FDictionary <> nil) or (Dictionaries.Count > 0)) then
  2543.   begin
  2544.     Translate;
  2545.   end;
  2546. end;
  2547.  
  2548. procedure TIvTranslator.Notification(component: TComponent; operation: TOperation);
  2549. begin
  2550.   inherited Notification(component, operation);
  2551.  
  2552.   if IsDesignTime and not (csReading in owner.ComponentState) and (operation = opInsert) then
  2553.   begin
  2554.     { Scans the component to add new targets }
  2555.  
  2556.     AddObjectTargets(component, FTargets, False);
  2557.   end;
  2558. end;
  2559.  
  2560. procedure TIvTranslator.ReadTargets(reader: TReader);
  2561. var
  2562.   i, version: Integer;
  2563.   target: TIvTargetProperty;
  2564. begin
  2565.   FTargets.ExcludeCount := 0;
  2566.   reader.ReadListBegin;
  2567.   version := reader.ReadInteger;
  2568.   if version > TARGETS_VERSION_C then
  2569.     raise EIvMulti.Create('Unknown target properties data version: ' + IntToStr(version));
  2570.  
  2571.   for i := 0 to reader.ReadInteger - 1 do
  2572.   begin
  2573.     { Reads the target from the stream }
  2574.  
  2575.     target := TIvTargetProperty.CreateDefault;
  2576.     reader.ReadListBegin;
  2577.     target.TargetClassName := reader.ReadString;
  2578.     target.TargetPropertyName := reader.ReadString;
  2579.     target.TargetType := TIvTargetType(reader.ReadInteger);
  2580.     reader.ReadListEnd;
  2581.  
  2582.     { Check if the target already exists. If not then adds the target. }
  2583.     
  2584.     if FTargets.Find(target) = -1 then
  2585.     begin
  2586.       if target.TargetType = ivttExclude then
  2587.         FTargets.ExcludeCount := FTargets.ExcludeCount + 1;
  2588.       FTargets.Add(target);
  2589.     end
  2590.     else
  2591.       target.Free;
  2592.   end;
  2593.   reader.ReadListEnd;
  2594. end;
  2595.  
  2596. procedure TIvTranslator.WriteTargets(writer: TWriter);
  2597. var
  2598.   i, j, count: Integer;
  2599.   exists: Boolean;
  2600.   targets: TIvTargetProperties;
  2601. begin
  2602. {$IFDEF WIN32}
  2603.   if (writer.Ancestor <> nil) and (writer.Ancestor is TIvTranslator) then
  2604.   begin
  2605.     targets := TIvTranslator(writer.Ancestor).Targets;
  2606.     count := 0;
  2607.     for i := 0 to FTargets.Count - 1 do
  2608.     begin
  2609.       { Checks of the parent form contains the target }
  2610.  
  2611.       exists := False;
  2612.       for j := 0 to targets.Count - 1 do
  2613.       begin
  2614.         if targets[j].IsEqual(FTargets[i]) then
  2615.         begin
  2616.           exists := True;
  2617.           Break;
  2618.         end;
  2619.       end;
  2620.  
  2621.       if not exists then
  2622.         Inc(count);
  2623.     end;
  2624.   end
  2625.   else
  2626. {$ENDIF}
  2627.   begin
  2628.     targets := nil;
  2629.     count := FTargets.Count;
  2630.   end;
  2631.  
  2632.   writer.WriteListBegin;
  2633.   writer.WriteInteger(TARGETS_VERSION_C);
  2634.   writer.WriteInteger(count);
  2635.   for i := 0 to FTargets.Count - 1 do
  2636.   begin
  2637.     { Checks of the parent form contains the target }
  2638.  
  2639.     exists := False;
  2640.     if targets <> nil then
  2641.     begin
  2642.       for j := 0 to targets.Count - 1 do
  2643.       begin
  2644.         if targets[j].IsEqual(FTargets[i]) then
  2645.         begin
  2646.           exists := True;
  2647.           Break;
  2648.         end;
  2649.       end;
  2650.     end;
  2651.  
  2652.     { Writes the target of the parent does not contain it. }
  2653.  
  2654.     if not exists then
  2655.     begin
  2656.       writer.WriteListBegin;
  2657.       with FTargets[i] do
  2658.       begin
  2659.         writer.WriteString(TargetClassName);
  2660.         writer.WriteString(TargetPropertyName);
  2661.         writer.WriteInteger(Integer(TargetType));
  2662.       end;
  2663.       writer.WriteListEnd;
  2664.     end;
  2665.   end;
  2666.   writer.WriteListEnd;
  2667. end;
  2668.  
  2669. procedure TIvTranslator.DefineProperties(filer: TFiler);
  2670.  
  2671.   function HasTargetsData: Boolean;
  2672.   begin
  2673.     if filer is TReader then
  2674.       Result := True
  2675.     else
  2676.     begin
  2677.       Result := FTargets.Count > 0;
  2678. {$IFDEF WIN32}
  2679.       if (filer.Ancestor <> nil) and (filer.Ancestor is TIvTranslator)then
  2680.         Result := Result and not FTargets.IsEqual(TIvTranslator(filer.Ancestor).Targets);
  2681. {$ENDIF}
  2682.     end;
  2683.   end;
  2684.  
  2685. begin
  2686.   inherited DefineProperties(filer);
  2687.  
  2688.   filer.DefineProperty('TargetsData', ReadTargets, WriteTargets, HasTargetsData);
  2689. end;
  2690.  
  2691. {$IFDEF WIN32}
  2692. { Return handle to a window containing the system menu }
  2693.  
  2694. function TIvTranslator.GetSystemMenuWinHandle: THandle;
  2695. begin
  2696.   { Return 0 if host does not have system menu }
  2697.  
  2698.   Result := 0;
  2699.   if Host is TWinControl then
  2700.   begin
  2701.     with TWinControl(Host) do
  2702.     begin
  2703.       if HandleAllocated then
  2704.         Result := Handle;
  2705.     end;
  2706.   end;
  2707. end;
  2708. {$ENDIF}
  2709.  
  2710.  
  2711. { Helper functions }
  2712.  
  2713. procedure IvCenterControl(parent, control: TControl);
  2714. begin
  2715.   if parent = nil then
  2716.   begin
  2717.     control.Left := (Screen.Width - control.Width) div 2;
  2718.     control.Top := (Screen.Height - control.Height) div 2;
  2719.   end
  2720.   else
  2721.   begin
  2722.     control.Left := parent.Left + (parent.Width - control.Width) div 2;
  2723.     control.Top := parent.Top + (parent.Height - control.Height) div 2;
  2724.   end;
  2725.  
  2726.   if control.Left < 0 then
  2727.     control.Left := 0;
  2728.  
  2729.   if control.Top < 0 then
  2730.     control.Top := 0;
  2731. end;
  2732.  
  2733. {$IFDEF WIN32}
  2734. initialization
  2735.   Modules := TIvModules.Create;
  2736. finalization
  2737.   Modules.Free;
  2738.   Modules := nil;
  2739. {$ELSE}
  2740. begin
  2741.   Modules := TIvModules.Create;
  2742. {$ENDIF}
  2743. end.
  2744.